package com.handsome.widgets.data;

import com.handsome.controller.consts.CommonConst;
import com.handsome.entity.HsDictionary;
import com.handsome.entity.HsMenu;
import com.handsome.service.AuthService;
import com.handsome.service.DictionaryService;
import com.handsome.widgets.OptionWidget;
import com.handsome.widgets.RadioWidget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Component
public class WidgetData {

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private AuthService authService;

    public Map mappingDictionaryFormData(HttpServletRequest request, HashMap<String, Object> map) {
        Map fieldAttrs = (Map) request.getAttribute(CommonConst.ENTITY_DATA_KEY);
        String uri = request.getRequestURI();
        String type = uri.substring(uri.lastIndexOf("/") + 1);
        List<OptionWidget> options = new ArrayList<>();
        List<HsDictionary> dictionaryItems = dictionaryService.getAllDictionary();
        Integer id = 0;
        if (type.equals(CommonConst.ACTION_TYPE_ADD)) {
            id = (Integer) fieldAttrs.get("id");
        }
        if (type.equals(CommonConst.ACTION_TYPE_EDIT)) {
            id = (Integer) fieldAttrs.get("parentId");
        }
        for (HsDictionary dictionaryItem : dictionaryItems) {
            OptionWidget widget = new OptionWidget();
            widget.setSelected(dictionaryItem.getId().equals(id));
            widget.setValue(dictionaryItem.getId());
            widget.setLabel(dictionaryItem.getTitle());
            widget.setParentId(dictionaryItem.getParentId());
            options.add(widget);
        }
        Map hashMap = new HashMap<>();
        hashMap.put("select", options);
        return hashMap;
    }

    public Map mappingMenuFormWidget(HttpServletRequest request, HashMap<String, Object> map) {

        // 获取菜单类型
        List<HsDictionary> menuTypes = dictionaryService.getDictionaryById(148);
        List<RadioWidget> radios = new ArrayList<>();

        for (HsDictionary menuType : menuTypes) {
            RadioWidget radio = new RadioWidget();
            radio.setDisabled(false);
            radio.setChecked(menuType.getTitle().equals("菜单"));
            radio.setLabel(menuType.getTitle());
            radio.setValue(menuType.getValue());
            radios.add(radio);
        }

        List<OptionWidget> options = new ArrayList<>();
        List<HsMenu> menus = authService.getMenus(false);
        for (HsMenu menu : menus) {
            OptionWidget option = new OptionWidget();
            option.setSelected(false);
            option.setValue(menu.getId());
            option.setLabel(menu.getTitle());
            option.setParentId(menu.getParentId());
            options.add(option);
        }

        Map hashMap = new HashMap<>();
        hashMap.put("select", options);
        hashMap.put("radio", radios);
        return hashMap;
    }


}
